package cn.edu.ctbu.demo1.webapi;

import cn.edu.ctbu.demo1.core.PageUtils;
import cn.edu.ctbu.demo1.core.RUtil;
import cn.edu.ctbu.demo1.domain.Dept;
import cn.edu.ctbu.demo1.service.DeptService;
import cn.edu.ctbu.demo1.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

@RestController
@RequestMapping("/webapi/dept")
public class DeptRestController {
    @Autowired
    DeptService deptService;
    /**
     * 读取的url:/webapi/dept/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public R get(@PathVariable Integer id){
        Dept dept = deptService.findById(id);
        return RUtil.success(dept);
    }

    /**
     * 新增学生的方法
     * @param dept
     * @return
     */
    @PostMapping("/insert")
    public R insert(Dept dept){

        Dept dept1 = deptService.insert(dept);
        return RUtil.success(dept1);
    }

    /**
     * 更新学生的方法
     * @param dept
     * @return
     */
    @PutMapping("/update")
    public R update(Dept dept){
        deptService.update(dept);
        return RUtil.success(dept);
    }
    /**
     * 删除学生的方法
     * @param id
     * @deturn
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        deptService.delete(id);
        return RUtil.success(null);
    }

    /**
     * 分页,读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public R getByPage(
            @RequestParam(value = "page",defaultValue ="0")Integer page,
            @RequestParam(value = "size",defaultValue ="10")Integer size,
            @RequestParam(value ="select",defaultValue = "")Integer select,
            @RequestParam(value ="content",defaultValue = "")String content
    ){
        Page<Dept> deptPage;
        /**
         * 如果没有查询就显示表中的所有内容
         */
        if(StringUtils.isEmpty((content))){
            Pageable pageable = PageRequest.of(page,size, Sort.by(Sort.Direction.ASC,"id"));
            deptPage = deptService.findAll(pageable);
        }
        else {
            Dept dept = new Dept();
            Pageable pageable = PageRequest.of(0,10, Sort.by(Sort.Direction.ASC,"id"));
            ExampleMatcher matcher;
            /**
             * 如果下拉框选中的是学院编号
             */
            if(select.equals(1))
            {
                dept.setNo(content);
                matcher = ExampleMatcher.matching().
                        withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());
            }
            /**
             * 如果下拉框选中的是学院名称
             */
            else {
                dept.setName(content);
                matcher = ExampleMatcher.matching()//模糊查询
                        .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            }

            Example<Dept> example = Example.of(dept,matcher);
            deptPage = deptService.findAll(example,pageable);
        }
        PageUtils pageUtils = new PageUtils(deptPage.getContent(),deptPage.getTotalElements());
        return RUtil.success(pageUtils);
    }
}
